#ifndef THREADSAFEQLIST_H
#define THREADSAFEQLIST_H

#include <QList>
#include <QObject>
#include <QReadWriteLock>
#include <QReadLocker>
#include <QWriteLocker>

using namespace std;

/*
  Template Class for thread safe access to a QList
  */

template <class T>
class ThreadSafeQList
{

public:
    ThreadSafeQList() {}

    T readIndex( int index )
    {
        QReadLocker locker( &rwLock );
        return list.at( index );
    }

    void writeIndex( int index, T item )
    {
        rwLock.lockForWrite();
        list[index] = item;
        rwLock.unlock();
    }

    void insert( T item )
    {
        rwLock.lockForWrite();
        list << item;
        rwLock.unlock();
    }

    const int size()
    {
        QReadLocker locker( &rwLock );
        return list.size();
    }

private:
    QList<T> list;
    QReadWriteLock rwLock;

};

/*
const T* ThreadSafeQList<T>::readIndex( int index )
{
    rwLock.lockForRead();
    const T* temp = list.at( index );
    rwLock.unlock();
    return temp;
}

void ThreadSafeQList<T>::writeIndex( int index, T item )
{
    rwLock.lockForWrite();
    list[index] = item;
    rwLock.unlock();
}

void ThreadSafeQList<T>::insert(T item)
{
    rwLock.lockForWrite();
    list << item;
    rwLock.unlock();
}

const int ThreadSafeQList::size()
{
    rwLock.lockForRead();
    list.size();
    rwLock.unlock();
}

T* ThreadSafeQList<T>::~ThreadSafeQList()
{
    while( !list.isEmpty() )
        delete list.takeFirst();
}
*/

#endif // THREADSAFEQLIST_H
